//
//  PopPay.m
//  仿支付宝密码输入框
//
//  Created by LH on 17/4/25.
//  Copyright © 2017年 LH. All rights reserved.
//

#import "TPSPushPay.h"
#import "AppDelegate.h"
#import "UIColor+Category.h"
#import "Masonry.h"
#import <pop/POP.h>
#define HEIGHT [UIScreen mainScreen].bounds.size.height
#define WIDTH [UIScreen mainScreen].bounds.size.width
@interface TPSPushPay()<UITextFieldDelegate>
{
    BOOL _isVisual;
}
@property (nonatomic,strong)UIView *mainView;
@property (nonatomic,strong)UIView *topView;
@property (nonatomic,strong)UITextField *passwordTF;
@property (nonatomic,strong)UIButton *forgetPassword;
@property (nonatomic,strong)UIImageView *img;
@property (nonatomic,strong)UILabel *titleLabel;

@property (nonatomic,copy)NSString *forgetStr;
@property (nonatomic,copy)NSString *titleStr;
@property (nonatomic,strong)UIViewController *pushVC;
@end

@implementation TPSPushPay

- (instancetype)init{
    if (self = [super init]) {
        //_isVisual=YES;
        self.frame=CGRectMake(0, 0,HEIGHT, WIDTH);
        self.backgroundColor=[[UIColor blackColor] colorWithAlphaComponent:0.4];
        
        [self.passwordTF becomeFirstResponder];
    }
    return self;
}
-(void)createView{
    
    [self addSubview:self.mainView];

    UIWindow * currentWindow1=  ((AppDelegate *)[UIApplication sharedApplication].delegate).window;
    [currentWindow1.rootViewController.view addSubview:self];
    //创建一个POPBasicAnimation动画 透明度的变化
    POPBasicAnimation *basicAnimation=[POPBasicAnimation animationWithPropertyNamed:kPOPViewAlpha];
    basicAnimation.fromValue=@(0);
    basicAnimation.toValue=@(1);
    basicAnimation.duration=0.6f; //设置动画的间隔时间 默认是0.4秒
    basicAnimation.repeatCount=0; //重复次数 HUGE_VALF设置为无限次重复
    [self pop_addAnimation:basicAnimation forKey:@"myViewAnimation"];
}

-(void)setTitle:(NSString *)titleStr forgetText:(NSString *)forgetText delegate:(id )fromVC;{
    self.titleStr=titleStr;
    self.forgetStr=forgetText;
    self.delegate=fromVC;
    _pushVC=fromVC;
    
    [self createView];
    [self.passwordTF becomeFirstResponder];
    _pushVC.view.backgroundColor=[UIColor whiteColor];
}
-(UIView *)mainView{
    if (!_mainView) {
        
        _mainView=[[UIView alloc]init];
        _mainView.frame=CGRectMake(0,HEIGHT*0.30 , WIDTH, HEIGHT*0.30);
        _mainView.backgroundColor=[UIColor whiteColor];
        [_mainView addSubview:self.topView];
        [_mainView addSubview:self.passwordTF];
        [_mainView addSubview:self.forgetPassword];
        
    }
    return _mainView;
}
-(UIView *)topView{
    if (!_topView) {
        _topView=[[UIView alloc]init];
        _topView.frame=CGRectMake(0, 0, WIDTH, 50);
        
        UIView *closeView=[[UIView alloc]init];
        closeView.frame=CGRectMake(0, 0, 50, 50);
        [_topView addSubview:closeView];
  
        UIButton *closeBut=[UIButton buttonWithType:UIButtonTypeCustom];
        closeBut.frame=CGRectMake(0, 0, 20, 20);
        closeBut.center = closeView.center;
        [closeBut setBackgroundImage:[UIImage imageNamed:@"close"] forState:UIControlStateNormal];
        [closeView addSubview:closeBut];
        
        UIButton *but=[UIButton buttonWithType:UIButtonTypeCustom];
        but.frame=closeView.frame;
        [closeView addSubview:but];
        [but addTarget:self action:@selector(closeView) forControlEvents:UIControlEventTouchUpInside];
        
        UIImageView *lineImage=[[UIImageView alloc]init];
        lineImage.frame=CGRectMake(0, 49, WIDTH, 1);
        lineImage.backgroundColor=[UIColor colorWithHexString:@"F5F5F5"];
        [_topView addSubview:lineImage];
        
        self.titleLabel=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, WIDTH-100, 20)];
        self.titleLabel.center=_topView.center;
        self.titleLabel.text=self.titleStr;
        self.titleLabel.textColor=[UIColor blackColor];
        self.titleLabel.textAlignment=NSTextAlignmentCenter;
        self.titleLabel.font=[UIFont systemFontOfSize:18];
        [_topView addSubview:self.titleLabel];
        
    }
    return _topView;
}
-(UITextField   *)passwordTF{
    if (!_passwordTF) {
        _passwordTF=[[UITextField alloc]initWithFrame:CGRectMake(10, 70, WIDTH-20, 44)];
        _passwordTF.delegate=self;
        _passwordTF.placeholder=@"请输入6-16位的密码";
        _passwordTF.keyboardType=UIKeyboardTypeAlphabet;
        _passwordTF.returnKeyType=UIReturnKeyDone;
        UIView *rightView=[[UIView alloc]initWithFrame:CGRectMake(WIDTH-20-40, 0, 30, 44)];
        _img = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Path 1578"]];
        _img.frame=CGRectMake(2.5, 17 , 25, 11);
        [rightView addSubview:_img];
        
        UIButton *but=[UIButton buttonWithType:UIButtonTypeCustom];
        but.frame=CGRectMake(0, 0, 30, 44);
        [rightView addSubview:but];
        [but addTarget:self action:@selector(secureTextEntry) forControlEvents:UIControlEventTouchUpInside];
        
        _passwordTF.rightView = rightView;
        _passwordTF.rightViewMode = UITextFieldViewModeAlways;
        _passwordTF.secureTextEntry = YES;
        _isVisual=YES;
        [_passwordTF setBorderStyle:UITextBorderStyleRoundedRect];
    }
    return _passwordTF;
}
-(UIButton *)forgetPassword{
    if (!_forgetPassword) {
        _forgetPassword=[UIButton buttonWithType:UIButtonTypeCustom];
        _forgetPassword.titleLabel.font = [UIFont systemFontOfSize:12.0];
        NSString *str=self.forgetStr;
        [_forgetPassword setTitle:str forState:UIControlStateNormal];
        [_forgetPassword setTitleColor:[UIColor colorWithHexString:@"00A1F5"] forState:UIControlStateNormal];
        //重要的是下面这部分哦！
        [_forgetPassword addTarget:self action:@selector(forgetPWBut) forControlEvents:UIControlEventTouchUpInside];
        CGSize titleSize = [str sizeWithAttributes:@{NSFontAttributeName: [UIFont fontWithName:_forgetPassword.titleLabel.font.fontName size:_forgetPassword.titleLabel.font.pointSize]}];
        
        titleSize.height = 20;
        titleSize.width += 20;
        
        _forgetPassword.frame = CGRectMake(WIDTH-titleSize.width-10, 130, titleSize.width, titleSize.height);
       
    }
    return _forgetPassword;
}

#pragma mark 是佛隐藏“忘记密码”按钮 默认不隐藏
-(void)setHideForgetPassword:(BOOL)hideForgetPassword{
    self.forgetPassword.hidden=hideForgetPassword;
}

#pragma mark 设置标题
-(void)setTitleText:(NSString *)titleText{
    self.titleLabel.text=titleText;
}

#pragma mark TextFieldDelegate
// 使用UITextField从明文切换到密文后，输入任何值都会将密文的输入先清空。这个是UITextField默认的设置，好像也没有一个属性值可以直接控制吧。不过在代理里面，加多一个判断也能避免密文清空的问题，如下：
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSString *toBeString = [textField.text stringByReplacingCharactersInRange:range withString:string];
    if (textField == self.passwordTF && textField.isSecureTextEntry) {
        textField.text = toBeString;
        return NO;
    }
    
    return YES;
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    //返回一个BOOL值，指明是否允许在按下回车键时结束编辑
    //如果允许要调用resignFirstResponder 方法，这回导致结束编辑，而键盘会被收起
    //[textField resignFirstResponder];//查一下resign这个单词的意思就明白这个方法了
    if (self.delegate && [self.delegate respondsToSelector:@selector(password:)]) {
        [self.delegate password:textField.text];
    }
    
    [self closeView];
    return YES;
}

#pragma mark 忘记密码
-(void)forgetPWBut{
    if (self.delegate && [self.delegate respondsToSelector:@selector(forgetPassword)]) {
        [self.delegate forgetPassword];
    }
    [self closeView];
   
}
#pragma mark 关闭
-(void)closeView{
    _pushVC.view.backgroundColor=[UIColor colorWithHexString:@"F4F4F4"];
    [self removeFromSuperview];
    self.mainView=nil;
}
#pragma mark 密码可视
-(void)secureTextEntry{
    _isVisual = !_isVisual;
    if (!_isVisual) {
        //明文
        _img.image=[UIImage imageNamed:@"Group 1970"];
        NSString *tempPwdStr = self.passwordTF.text;
        self.passwordTF.text = @""; // 这句代码可以防止切换的时候光标偏移
        self.passwordTF.secureTextEntry = NO;
        self.passwordTF.text = tempPwdStr;
    }else{
        _img.image=[UIImage imageNamed:@"Path 1578"];
        NSString *tempPwdStr = self.passwordTF.text;
        self.passwordTF.text = @""; // 这句代码可以防止切换的时候光标偏移
        self.passwordTF.secureTextEntry = YES;
        self.passwordTF.text = tempPwdStr;
    }
    self.passwordTF.secureTextEntry = _isVisual;

}

@end


